/* canvas.cpp */

#include <dodo_plot/kernel/canvas.h>
#include <dodo_plot/kernel/picture.h>
#include <dodo_plot/kernel/basic_elements.h>

namespace dodo_plot
{

Canvas::Canvas(int x, int y) : x_(x), y_(y) 
{
	surface_ = Cairo::ImageSurface::create(Cairo::FORMAT_ARGB32, x, y);
	cr_ = Cairo::Context::create(surface_);
	cr_->set_line_cap(Cairo::LINE_CAP_ROUND);
	cr_->set_line_join(Cairo::LINE_JOIN_ROUND);
}

Canvas::~Canvas()
{
}
		
void Canvas::write_png(const std::string& filename) const
{
	surface_->write_to_png(filename);
}

void Canvas::draw(const Picture& picture)
{
	// clear the canvas
	
	cr_->save();
	cr_->set_source_rgb(1.0, 1.0, 1.0);
	cr_->paint();
	cr_->restore();

	Position pos(0.0, x_, 0.0, y_);
	Plotter plotter(cr_, pos, picture.local_pos());
	picture.draw(plotter);
}

void Canvas::resize(int new_x, int new_y)
{
	surface_ = Cairo::ImageSurface::create(Cairo::FORMAT_ARGB32, new_x, new_y);
	cr_ = Cairo::Context::create(surface_);
	x_ = new_x;
	y_ = new_y;
}

void Canvas::resize_as(const Picture& picture)
{
	resize(picture.width(), picture.height());
}

}
